package com.mapbased.cc.form;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import com.mapbased.cc.valid.*;
import com.mapbased.wfw.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author Chang Huanyou (Baily)
 * @version 1.0
 */
public class FormElement
{
    protected String name;
    protected String caption;

    protected Object[] describe=NO_DESC;

    public static final Object[] NO_DESC=new Object[]
        {};

    protected String msg;
    protected String exp;
    protected String css;
    protected String style;

    Validator validator;
    protected String events;
    public void set(String name,String caption,
                    String msg,String exp,String css,String style)
    {
        this.name=name;
        this.caption=caption;
        this.msg=msg;
        this.css=css;
        this.style=style;
        if(exp != null)
        {
            this.validator=ValidatorCreator.create(exp);
        }

    }

    final public void init(Element e,PageLoader.LoadContext lc)
    {

        this.caption=(e.attributeValue("caption"));
        this.msg=e.attributeValue("msg");
        this.exp=e.attributeValue("exp");
        this.style=e.attributeValue("style");
        if(this.exp != null)
        {
            this.validator=ValidatorCreator.create(exp);

        }

        this.css=e.attributeValue("class");
        this.name=(e.attributeValue("name"));
        String hidedesc=e.attributeValue("hidedesc");

        StringBuffer sb=new StringBuffer();
        List attributes=e.attributes();
        for(int i=0;i < attributes.size();i++)
        {
            Attribute a=(Attribute)attributes.get(i);
            String name=a.getName();
            if(name.startsWith("on"))
            {
                sb.append(name).append("=\"").append(
                    com.mapbased.wfw.Util.replaceToXmlPreTextarea(
                        a.getValue()))
                    .append("\" ");
            }
        }
        this.events=sb.toString();

        if(hidedesc != null)
        {
            this.describe=NO_DESC;
        }
        else
        {
            String s=e.attributeValue("desc");
            if(s != null)
            {
                this.describe=new String[]
                    {
                    s
                };
            }
            else
            {
                this.describe=Component.EMPTYSEGS;
            }

            //  this.initValue=("true".equalsIgnoreCase(e.attributeValue("init")));
            Element desc=e.element("desc");
            if(desc != null)
            {
                PageLoader.LoadContext nlc=new PageLoader.LoadContext(lc);
                PageLoader.StringBufferBag bag=nlc.last;
                List segs=nlc.segments;
                lc.loader.parseElementChildren(desc,nlc);
                if(bag.buffer.length() > 0)
                {
                    segs.add(bag.getString());
                }
                this.describe=segs.toArray();

            }
        }
        this.initCustom(e,lc);
    }

    protected String getType()
    {
        return "text";
    }

    public void renderInputPart(RenderContext rc)
        throws IOException
    {
        java.io.Writer w=rc.getOutputWriter();
        w.write("<input class=\"");
        w.write(this.css == null ? this.getType() : this.css);

        if(this.style != null)
        {
            w.write("\" style=\"");
            w.write(this.style);

        }

        w.write("\" type=\"");
        w.write(this.getType());

        w.write("\" name=\"");

        w.write(this.name);

        w.write("\" id=\"");

        w.write(this.name);

        w.write("\" ");

        if(this.events != null)
        {
            w.write(this.events);
        }

        String value=rc.getSureSingleParam(this.name);
        if(!value.equals(""))
        {
            w.write("value=\"");
            value=com.mapbased.wfw.Util.replaceToXmlPreTextarea(value);
            w.write(value);
            w.write("\"");

        }
        w.write(" />");

    }

    protected void initCustom(Element e,PageLoader.LoadContext pl)
    {}

    public void validate(RenderContext rc)
    {
        if(this.validator != null)
        {
            if(!this.validator.isValid(rc.getSureSingleParam(this.name),rc))
            {
                rc.putError(this.name,
                            this.msg == null ? new StringBuffer()
                            .append(this.caption).append(" : ").append(this.
                    validator).toString() :
                            this.msg);
            }

        }
    }

    public String getCaption()
    {
        return caption;
    }

    public Object[] getDescribe()
    {
        return describe;
    }

    public String getName()
    {
        return name;
    }

    public String getEvents()
    {
        return events;
    }

    public void setCaption(String caption)
    {
        this.caption=caption;
    }

    public void setEvents(String events)
    {
        if(!events.endsWith(" "))
        {
            this.events=events + " ";
        }
        else
        {

            this.events=events;
        }

    }

}
